<?php

	if(!defined('YMT')) exit;

	/**
	 * @package Yomm
	 * @author bsuter
	 *
	 */
	Class Yomm_Boot {
		
		private $_boot_controller = null;
		
		private $_boot_method = null;
		
		private $_boot_file = null;
		
		private $_boot_file_class = null;
		
		private $_boot_object = null;
		
		public function __construct()
		{
			// set the controller variable: $_boot_controller
			if(array_key_exists(Yomm_Registry::get("boot_controller_param"), $_GET))
			{
				$this->_boot_controller = $_GET[Yomm_Registry::get("boot_controller_param")];
			} else {
				$this->_boot_controller = Yomm_Registry::get("boot_controller_default");
			}

			// set the method variable of the controller: $_boot_method
			if(array_key_exists(Yomm_Registry::get("boot_controller_method_param"), $_GET))
			{
				$this->_boot_method = $_GET[Yomm_Registry::get("boot_controller_method_param")];
			} else {
				$this->_boot_method = Yomm_Registry::get("boot_controller_method_default");
			}
		}
		
		public function doReturn($static_controller = null, $static_controller_method = null)
		{
			if(!is_null($static_controller)) $this->_boot_controller = $static_controller;

			if(!is_null($static_controller_method)) $this->_boot_method = $static_controller_method;

			$this->_boot_file = ucfirst($this->_boot_controller) . "Controller.class.php";
			
			$this->_boot_file_class = ucfirst($this->_boot_controller) . "Controller";
			
			if(!include_once(Yomm_Config::DIR_CONTROLLERS . $this->_boot_file))
			{
				throw new Yomm_Error("The boot_class_file {$this->_boot_file} was not found!");
			}
			
			$this->_boot_object = new $this->_boot_file_class();
			
			$_boot_object_output = call_user_func_array(array($this->_boot_object, $this->_boot_method), array());
			
			if($this->_boot_object->compression === true)
			{
				$_boot_object_output = $this->compressContent($_boot_object_output);
			}
			
			return $_boot_object_output;
		}
		
		private function compressContent($content)
		{
			return preg_replace(array('/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s'), array('>', '<', '\\1'), $content);
		}
		
		public function getView()
		{
			return $this->_boot_object->view;
		}
		
	}